package com.itheima.entity;

import com.hzheima.util.ImageUtil;
import com.itheima.config.AppConfig;

import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

public class Ball extends FIyingObject {
    //水平方向的速度
    private int xSpeed;
    //竖直方向的速度
    private int ySpeed;
    private int startLoc;
    private boolean startDown;
    private Random random = new Random();
    //血量
    private int blood;
    private int initBlood;

    public Ball(int x, int y, int ySpeed, int blood) {
        //随机图片
        int i = random.nextInt(15);
        img = ImageUtil.getImg("c" + i + ".png");
        //设置宽高
        width = img.getWidth();
        height = img.getHeight();
        //设置位置
        this.x = x;
        this.y = y;
        //设置速度
        this.xSpeed = random.nextInt(11) - 5;
        this.ySpeed = ySpeed;
        //设置血量
        this.blood = blood;
        this.initBlood = this.blood;
        //分裂的球直接可以向下移动
        startDown = true;
    }

    public int getInitBlood() {
        return initBlood;
    }

    public void setInitBlood(int initBlood) {
        this.initBlood = initBlood;
    }

    public int getBlood() {
        return blood;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }

    public Ball() {

        //随机图片
        int i = random.nextInt(15);
        img = ImageUtil.getImg("c" + i + ".png");
        //设置宽高
        width = img.getWidth();
        height = img.getHeight();

        //随机位置
        startLoc = random.nextInt(2);
        //设置x,y
        if (startLoc == 0) {
            //随机生成在左边
            x = -width;
            //设置速度
            xSpeed = 5;

        } else if (startLoc == 1) {
            //随机生成在右边
            x = AppConfig.APP_WIDTH;
            //设置速度
            xSpeed = -5;
        }
        y = 100;
        ySpeed = 5;

        //随机血量
        blood = random.nextInt(15) + 1;
        initBlood = blood;

    }

    public void move() {
        //水平移动
        this.x += xSpeed;
        //如果移动到屏幕一半左右就能有向下的速度
        if (startLoc == 0) {
            //说明是从左边出来的
            if (x >= 250) {
                //可以向下移动了
                startDown = true;
            }
        } else if (startLoc == 1) {
            //说明是从右边出来的
            if (x <= 250) {
                //可以向下移动了
                startDown = true;
            }
        }
        if (startDown) {
            this.y += ySpeed;
        }
        if (xSpeed < 0 && x <= 0) {
            //撞左墙
            xSpeed = -xSpeed;
        }
        if (xSpeed > 0 && x >= AppConfig.APP_WIDTH - width) {
            //撞右墙
            xSpeed = -xSpeed;
        }

        if (ySpeed < 0 && y <= 0) {
            //撞上墙
            ySpeed = -ySpeed;
        }

        if (ySpeed > 0 && y >= AppConfig.APP_HEIGHT - 40 - height) {
            //撞下墙
            ySpeed = -ySpeed;
        }
    }

    @Override
    public void draw(Graphics g) {
        super.draw(g);
        //绘制血量
        g.setColor(Color.white);
        g.setFont(new Font("楷体", Font.BOLD, 30));
        //如果血量>=10  就偏移  15  如果没有就偏移 10
        int dx = blood >= 10 ? 18 : 10;
        g.drawString(String.valueOf(blood), x + width / 2 - dx, y + height / 2 + 10);
    }

    public boolean shootBy(Bullet bullet) {
        //        shootBy 可以判断是否和子弹碰撞了，如果碰撞了，血量减少
        boolean flag = bullet.y < this.y + this.height && bullet.y + bullet.height > this.y
                && bullet.x + bullet.width > this.x && bullet.x < this.x + this.width;

        if (flag) {
            this.blood--;
        }
        return flag;
    }

    //球分裂
    public ArrayList<Ball> split() {
        ArrayList<Ball> balls = new ArrayList<>();
        if (this.blood > 0) {
            Ball b1 = new Ball(this.x, this.y, this.ySpeed, this.blood);
            Ball b2 = new Ball(this.x, this.y, this.ySpeed, this.blood);
            balls.add(b1);
            balls.add(b2);
        }
        return balls;
    }
}
